<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is a sample module for PyroCMS
 *
 * @author 		Jerel Unruh - PyroCMS Dev Team
 * @website		http://unruhdesigns.com
 * @package 	PyroCMS
 * @subpackage 	Sample Module
 */
class Admin extends Admin_Controller
{
	protected $section = 'tax_balance';
	protected $_ci;
	protected $_currentUserGroup;
	protected $_currentUserName = '';
	protected $_curentUserZones;
	public function __construct()
	{
		parent::__construct();
		
		$this->_ci = &get_instance();
		$this->lang->load('tax_balance');
		$this->_currentUserGroup = $this->_ci->ion_auth->get_user()->group;
		$this->_currentUserName = $this->_ci->ion_auth->get_user()->username;
		$this->_curentUserZones = $this->_ci->escort_user_roles_n_zones_entity->getUserRolesNZones($this->_currentUserName);
		
		$this->template->set_partial('shortcuts', 'admin/partials/shortcuts')
		
						->append_metadata(js('https://www.google.com/jsapi'))
						->append_metadata(js('escort.tax.balance.js', 'tax_balance'))
						->append_metadata(css('escort.tax.balance.css', 'tax_balance'));
	}

	/**
	 * List all items
	 */
	public function index()
	{	
		//do we need to unset the layout because the request is ajax?
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';
		//$escortIdList = $this->_ci->user_entity->getEscortIdListByAdmin($this->_currentUserName, $this->_currentUserGroup , $this->_curentUserZones);
				
		$this->template
			->title($this->module_details['name'])
			->append_metadata(js('admin/filter.js'))
			->set('userGroup', $this->_currentUserGroup);
		$this->template->build('admin/index', $this->data);
	
	}
	public function balance_data(){
		$fromDate = $this->input->post('from_date');
		$toDate = $this->input->post('to_date');
		$timeData = calculateTimeBetweenTwoDay($fromDate, $toDate);
		$result = array();
		$result['title'] = lang('escort_tax_balance_balance_report_label');
		$result['y'] = lang('escort_tax_balance_update_invoices_chart_comming_money_label');
		$result['y1'] = lang('escort_tax_balance_update_invoices_chart_expense_label');
		$result['y2'] = lang('escort_tax_balance_update_invoices_chart_balance_label');
		
		$result['time'] = $timeData;
		$invoiceData = array();
		if($timeData['years'] > 2){
		$result['x'] = lang('escort_tax_balance_update_invoices_chart_month_label');
			for($i = 0; $i < $timeData['diffYears']; $i++){
				if($i == 0){
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfYear(date('y',strtotime($fromDate)));
				}else{
					$nexYear = format_date(strtotime('+'.$i.' year', strtotime($fromDate)));
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfYear(date('y',strtotime($nexYear)));	
				}
				$money = 0;
				if($invoiceList){
					foreach ($invoiceList as $invoice){
						$lineList = $this->_ci->invoice_line_entity->getListByInvoice($invoice->id);
						if($lineList){
							foreach ($lineList as $line){
								$money += $line->price*$line->quantity*(100 + $line->tax) /100; 
							}
						}
					}
				}
				if($i == 0){
					$expense = $this->_ci->expense_entity->getExpenseOfYear(date('Y',strtotime($fromDate)));
					$invoiceData[] = array(date('Y',strtotime($fromDate)), $money, $expense, $money - $expense);
				}else{
					$expense = $this->_ci->expense_entity->getExpenseOfYear(date('Y',strtotime($nexYear)));
					$invoiceData[] = array(date('Y',strtotime($nexYear)), $money, $expense, $money - $expense);
				}
			}			
		}else if($timeData['diffDays'] > 31){
			$result['x'] = lang('escort_tax_balance_update_invoices_chart_month_label');
			for($i = 0; $i < $timeData['diffMonths']; $i++){
				if($i == 0){
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfMonth(date('m',strtotime($fromDate)), date('y',strtotime($fromDate)));
				}else{
					$nexMonth = format_date(strtotime('+'.$i.' month', strtotime($fromDate)));
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfMonth(date('m',strtotime($nexMonth)), date('y',strtotime($nexMonth)));	
				}
				$money = 0;
				if($invoiceList){
					foreach ($invoiceList as $invoice){
						$lineList = $this->_ci->invoice_line_entity->getListByInvoice($invoice->id);
						if($lineList){
							foreach ($lineList as $line){
								$money += $line->price*$line->quantity*(100 + $line->tax) /100; 
							}
						}
					}
				}
				if($i == 0){
					$expense = $this->_ci->expense_entity->getExpenseOfMonth(date('m',strtotime($fromDate)), date('y',strtotime($fromDate)));
					$invoiceData[] = array(date('m/Y',strtotime($fromDate)), $money, $expense, $money - $expense);
				}else{
					$expense = $this->_ci->expense_entity->getExpenseOfMonth(date('m',strtotime($nexMonth)), date('y',strtotime($nexMonth)));
					$invoiceData[] = array(date('m/Y',strtotime($nexMonth)), $money, $expense, $money - $expense);
				}
			}
		}else{
			$result['x'] = lang('escort_tax_balance_update_invoices_chart_date_label');
			for($i = 0; $i < $timeData['diffDays']; $i++){
				if($i == 0){
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfDay($fromDate);
				}else{
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfDay(format_date(strtotime('+'.$i.' day', strtotime($fromDate))));	
				}
				
				$money = 0;
				if($invoiceList){
					foreach ($invoiceList as $invoice){
						$lineList = $this->_ci->invoice_line_entity->getListByInvoice($invoice->id);
						if($lineList){
							foreach ($lineList as $line){
								$money += $line->price*$line->quantity*(100 + $line->tax) /100; 
							}
						}
					}
				}
				if($i == 0){
					$expense = $this->_ci->expense_entity->getExpenseOfDate($fromDate);
					$invoiceData[] = array($fromDate, $money, $expense, $money - $expense);
				}else{
					$expense = $this->_ci->expense_entity->getExpenseOfDate(format_date(strtotime('+'.$i.' day', strtotime($fromDate))));
					$invoiceData[] = array(format_date(strtotime('+'.$i.' day', strtotime($fromDate))), $money, $expense, $money - $expense);
				}
			}
		}
		$result['data'] = $invoiceData;
		echo json_encode($result);
	}
	public function tax_data(){
		$fromDate = $this->input->post('from_date');
		$toDate = $this->input->post('to_date');
		$timeData = calculateTimeBetweenTwoDay($fromDate, $toDate);
		$result = array();
		$result['title'] = lang('escort_tax_balance_tax_report_title');
		$result['y'] = lang('escort_tax_balance_update_invoices_chart_tax_label');
		$result['time'] = $timeData;
		$taxtData = array();
		if($timeData['years'] > 2){
			$result['x'] = lang('escort_tax_balance_update_invoices_chart_month_label');
			for($i = 0; $i < $timeData['diffYears']; $i++){
				if($i == 0){
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfYear(date('y',strtotime($fromDate)));
				}else{
					$nexYear = format_date(strtotime('+'.$i.' year', strtotime($fromDate)));
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfYear(date('y',strtotime($nexYear)));	
				}
				$money = 0;
				if($invoiceList){
					foreach ($invoiceList as $invoice){
						$tax = 18;
						if($invoice->user_id){
							$USER = $this->_ci->user_entity->init('id', $invoice->user_id);
							if($USER){
								$countrySetting = $this->_ci->country_setting_entity->init('country_id', $USER->country_id);
								if($countrySetting){
									$tax = $countrySetting->tax;
								}
							}
						}
						$lineList = $this->_ci->invoice_line_entity->getListByInvoice($invoice->id);
						if($lineList){
							foreach ($lineList as $line){
								$money += round(($line->price*$line->quantity*$tax *100)/(100 + $tax), 2);
							}
						}
					}
				}
				if($i == 0){
					$taxtData[] = array(date('Y',strtotime($fromDate)), $money);
				}else{
					$taxtData[] = array(date('Y',strtotime($nexYear)), $money);
				}
			}			
		}else if($timeData['diffDays'] > 31){
			$result['x'] = lang('escort_tax_balance_update_invoices_chart_month_label');
			for($i = 0; $i < $timeData['diffMonths']; $i++){
				if($i == 0){
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfMonth(date('m',strtotime($fromDate)), date('y',strtotime($fromDate)));
				}else{
					$nexMonth = format_date(strtotime('+'.$i.' month', strtotime($fromDate)));
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfMonth(date('m',strtotime($nexMonth)), date('y',strtotime($nexMonth)));	
				}
				$money = 0;
				if($invoiceList){
					foreach ($invoiceList as $invoice){
						$tax = 18;
						if($invoice->user_id){
							$USER = $this->_ci->user_entity->init('id', $invoice->user_id);
							if($USER){
								$countrySetting = $this->_ci->country_setting_entity->init('country_id', $USER->country_id);
								if($countrySetting){
									$tax = $countrySetting->tax;
								}
							}
						}
						$lineList = $this->_ci->invoice_line_entity->getListByInvoice($invoice->id);
						if($lineList){
							foreach ($lineList as $line){
								$money += round(($line->price*$line->quantity*$tax *100)/(100 + $tax), 2);
							}
						}
					}
				}
				if($i == 0){
					$taxtData[] = array(date('m/Y',strtotime($fromDate)), $money);
				}else{
					$taxtData[] = array(date('m/Y',strtotime($nexMonth)), $money);
				}
			}
		}else{
			$result['x'] = lang('escort_tax_balance_update_invoices_chart_date_label');
			for($i = 0; $i < $timeData['diffDays']; $i++){
				if($i == 0){
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfDay($fromDate);
				}else{
					$invoiceList = $this->_ci->invoice_entity->getInvoiceListOfDay(format_date(strtotime('+'.$i.' day', strtotime($fromDate))));	
				}
				
				$money = 0;
				if($invoiceList){
					foreach ($invoiceList as $invoice){
						$tax = 18;
						if($invoice->user_id){
							$USER = $this->_ci->user_entity->init('id', $invoice->user_id);
							if($USER){
								$countrySetting = $this->_ci->country_setting_entity->init('country_id', $USER->country_id);
								if($countrySetting){
									$tax = $countrySetting->tax;
								}
							}
						}
						
						$lineList = $this->_ci->invoice_line_entity->getListByInvoice($invoice->id);
						if($lineList){
							foreach ($lineList as $line){
								$money += round(($line->price*$line->quantity*$tax)/(100 + $tax), 2); 
							}
						}
					}
				}
				if($i == 0){
					$taxtData[] = array($fromDate, $money);
				}else{
					$taxtData[] = array(format_date(strtotime('+'.$i.' day', strtotime($fromDate))), $money);
				}
			}
		}
		$result['data'] = $taxtData;
		echo json_encode($result);
	}
}
